<?php
/* Immanuel192 Fix this module on 8/12/2009 */
class albumhomepagemodule {
	function name() { return "Album preview on homepage  Module"; }
	function description() { return "Show album preview on homepage"; }
	function author() { return "Immanuel192"; }
	
	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = "") {
		return array(
			'administrate'=>'Administrate',
			'configure'=>'Configure',
			'manage'=>'Manage Articles',
		);
	}
		
	function show($view,$loc = null, $title = "") {
		include_once(BASE . "modules/albummodule/core.php");
		include_once(BASE . "subsystems/getid3/getid3.php");
		$getid3 = new getID3();
		
		global $db;
		$config = $db->selectObject('albumhomepagemodule_config',"location_data='".serialize($loc)."'");
		if ($config == null) {
			$config->items = 1;
			$config->album_id=0;
		} 
		
		$album=$db->selectObject("album","id = {$config->album_id}");
		if (!$album) return;
		
		// get media items
		$sql="SELECT * FROM {$db->prefix}musics WHERE (album_id = {$config->album_id}) ORDER BY rank DESC LIMIT 0,{$config->items}";
		$musics=$db->selectObjectsBySql($sql);
		for($j=0; $j<count($musics); $j++) {
			  // fix music info
				$musics[$j]->downloadable=$album->downloadable;
				$musics[$j]->singer_search = htmlspecialchars($musics[$j]->singer);
				if ($musics[$j]->high_quality <= 0) {
					$musics[$j]->high_quality = '';
				} else {			
					$file = $db->selectObject('file', 'id='.$musics[$j]->high_quality);
					$musics[$j]->high_quality_id=$musics[$j]->high_quality;
					$musics[$j]->high_quality = $file->directory.'/'.$file->filename;
				if (file_exists($musics[$j]->high_quality))
				{
				  $file_info = $getid3->analyze($musics[$j]->high_quality); 				  
				  //getid3_lib::CopyTagsToComments($file_info); 
				  $musics[$j]->bitrate=round($file_info['bitrate']/1000,0);
				  $musics[$j]->fileformat=$file_info['fileformat'];
				  $file_info=$file_info['playtime_string'];
				  $musics[$j]->file_length = $file_info;
				 }
				}
			}
		
		// Create the template
		if ($album->media_preview == 2)
			$view="_video";
		else
			$view="_audio";
		$template = new template("albumhomepagemodule",$view,$loc);
		$template->register_permissions(
			array('administrate','configure','manage'),
			$loc);
		$template->assign('musics', $musics);
		$template->assign('config', $config);
		$file_name=create_playlist($musics);
		$template->assign('plist',$file_name);
		$template->assign('moduletitle',$title);
		$template->output();
	}
	
	function deleteIn($loc) {
		global $db;
		$db->delete('article',"location_data='".serialize($loc)."'");
		$db->delete('category', "location_data='".serialize($loc)."'");
		$db->delete('articlemodule_config', "location_data='".serialize($loc)."'");
	}
	
	function copyContent($oloc,$nloc) {
		global $db;
		$catid_map = array();
		
		foreach ($db->selectObjects('category',"location_data='".serialize($oloc)."'") as $category) {
			$oldid = $category->id;
			unset($category->id);
			$category->location_data = serialize($nloc);
			$catid_map[$oldid] = $db->insertObject($category,'category');
		}
		
		foreach ($db->selectObjects('article',"location_data='".serialize($oloc)."'") as $object) {
			unset($object->id);
			$object->location_data = serialize($nloc);
			if (isset($catid_map[$object->category_id])) {
				$object->category_id = $catid_map[$object->category_id];
			} else {
				$object->category_id = 0;
			}
			$db->insertObject($object,'article');
		}
		
		
		$conf = $db->selectObject('articlemodule_config', "location_data='".serialize($oloc)."'");
		unset($conf->id);
		$conf->location_data = serialize($nloc);
		$db->insertObject($conf,'articlemodule_config');
	}
}

?>
